home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / xlib / simplest.c.z / simplest.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  2.3 KB  |  102 lines

  1. /*
  2.  * simplest - simple single buffered RGBA xlib program.
  3.  */
  4. /* compile: cc -o simplest simplest.c -lGL -lX11 */
  5.  
  6. #include <GL/glx.h>
  7. #include <X11/keysym.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10.  
  11. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, None };
  12.  
  13. static void
  14. draw_scene(void) {
  15.     glClearColor(0.5, 0.5, 0.5, 1.0);
  16.     glClear(GL_COLOR_BUFFER_BIT);
  17.     glColor3f(1.0,0.0,0.0);
  18.     glRectf(-.5,-.5,.5,.5);
  19.     glColor3f(0.0,1.0,0.0);
  20.     glRectf(-.4,-.4,.4,.4);
  21.     glColor3f(0.0,0.0,1.0);
  22.     glRectf(-.3,-.3,.3,.3);
  23.     glFlush();
  24. }
  25.  
  26. static void
  27. process_input(Display *dpy) {
  28.     XEvent event;
  29.     Bool redraw = 0;
  30.  
  31.     do {
  32.     char buf[31];
  33.     KeySym keysym;
  34.  
  35.     XNextEvent(dpy, &event);
  36.     switch(event.type) {
  37.     case Expose:
  38.         redraw = 1;
  39.         break;
  40.     case ConfigureNotify:
  41.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  42.         redraw = 1;
  43.         break;
  44.     case KeyPress:
  45.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  46.         switch (keysym) {
  47.         case XK_Escape:
  48.         exit(EXIT_SUCCESS);
  49.         default:
  50.         break;
  51.         }
  52.     default:
  53.         break;
  54.     }
  55.     } while (XPending(dpy));
  56.     if (redraw) draw_scene();
  57. }
  58.  
  59. static void
  60. error(const char *prog, const char *msg) {
  61.     fprintf(stderr, "%s: %s\n", prog, msg);
  62.     exit(EXIT_FAILURE);
  63. }
  64.  
  65. int
  66. main(int argc, char **argv) {
  67.     Display *dpy;
  68.     XVisualInfo *vi;
  69.     XSetWindowAttributes swa;
  70.     Window win;
  71.     GLXContext cx;
  72.  
  73.     /* get a connection */
  74.     dpy = XOpenDisplay(0);
  75.     if (!dpy) error(argv[0], "can't open display");
  76.  
  77.     /* get an appropriate visual */
  78.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  79.     if (!vi) error(argv[0], "no suitable visual");
  80.  
  81.     /* create a GLX context */
  82.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  83.  
  84.     /* create a color map */
  85.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  86.                                    vi->visual, AllocNone);
  87.  
  88.     /* create a window */
  89.     swa.border_pixel = 0;
  90.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  91.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300,
  92.             0, vi->depth, InputOutput, vi->visual,
  93.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  94.     XStoreName(dpy, win, "simplest");
  95.     XMapWindow(dpy, win);
  96.  
  97.     /* connect the context to the window */
  98.     glXMakeCurrent(dpy, win, cx);
  99.  
  100.     while (1) process_input(dpy);
  101. }
  102.